#!/usr/bin/env python
#
#
#  (C) COPYRIGHT 2009, Brian Knobbs <brian@robotpenguin.com>
#  
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

""" mySlacker

This script creates a window with webkit embedded that shows the
player from slacker.com. """


import sys, os, pygtk, gtk
pygtk.require('2.0')

#setup libsoup libraries for cookie persistance
import ctypes
try:
	libsoup = ctypes.CDLL('/usr/lib/libsoup-2.4.so.1')
	libwebkit = ctypes.CDLL('/usr/lib/libwebkitgtk-1.0.so.0')
except:
	print ' Could not load needed libraries `/usr/lib/libsoup-2.4.so.1` and `/usr/lib/libwebgtk-1.0.so.0`'
	print ' Try installing libsoup2.4-1 and libwebkitgtk-1.0-0'
	sys.exit(1)

#import webkit library
try:
    import webkit
except:
    print ' Could not find webkit'
    print ' please install python-webkit'
    sys.exit(1)


class mySlacker:
    def __init__(self):
	self.setup_cookie_session()

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("mySlacker")
        self.window.set_size_request(650,500)
        self.window.set_resizable(False)
        self.window.connect("destroy", lambda w: gtk.main_quit())
        self.tooltips = gtk.Tooltips()
        self.tooltips.set_tip(self.window, "mySlacker")
        
        self.view = webkit.WebView()        
        self.view.connect('navigation-policy-decision-requested', self.navigation_policy_decision_requested_cb)

        self.mainFrame = self.view.get_main_frame()
        self.mainFrame.load_uri("http://www.slacker.com/webplayer/index_embed.vm")
        
        self.window.add(self.view)
        self.window.show_all() 
        self.window.show()
        
    def navigation_policy_decision_requested_cb(self, view, frame, net_req, nav_act, pol_dec):
        uri=net_req.get_uri()
        
        '''Prevent the browser from redirecting back to the slacker homepage.'''
        if (uri == 'http://www.slacker.com/'):
            pol_dec.ignore()
            return True
        
        return False
    
    def setup_cookie_session(self):
        session = libwebkit.webkit_get_default_session()

        #get safe directory
        homeDir = os.getenv('HOME')
        cookieFolder = homeDir + '/.mySlacker'
        if not os.path.isdir(cookieFolder):
            os.mkdir(cookieFolder)

        #add a new persistent cookie jar
        cookiejar = libsoup.soup_cookie_jar_text_new(cookieFolder + '/cookies.txt',False)
        libsoup.soup_session_add_feature(session, cookiejar)


def main():
    gtk.main()

if __name__ == "__main__":
    mySlackerWindow = mySlacker()
    main()

sys.exit()
